﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Data;

using BusinessManagementStudio.Models.BusinessObject.Entities;
using BusinessManagementStudio.Models.BusinessObject.Reports;

namespace BusinessManagementStudio.Models.BusinessObject.Services
{
    public class ReportService
    {
        private QuotationService quoService = new QuotationService();
        private InvoiceService jllPOInvoceService = new InvoiceService();
        private ContractorAssignmentService contAssignService = new ContractorAssignmentService();

        public List<IData> GetAllInvoices(List<Invoice> jllPOInvoiceList)
        {
            int count = 0;
            List<IData> data = new List<IData>();

            if (jllPOInvoiceList.Count > 0)
            {
                for (int i = 0; i < jllPOInvoiceList.Count; i++)
                {
                    IData tempData = new IData();
                    tempData.ID = i + 1;

                    DateTime QuotationDate = quoService.GetOrderQuotation_ByQuotationNo(jllPOInvoiceList[i].QuotationNo)[0].QuotationCreationDate;

                    tempData.RefNo = jllPOInvoiceList[i].InvoiceDate.Month.ToString("00") + jllPOInvoiceList[i].InvoiceDate.Day.ToString("00");
                    tempData.InvoiceNo = jllPOInvoiceList[i].InvoiceNo;
                    tempData.QuotationNo = jllPOInvoiceList[i].QuotationNo;
                    tempData.Description = jllPOInvoiceList[i].Location;
                    tempData.InvoiceDate = jllPOInvoiceList[i].InvoiceDate;
                    tempData.WorkOrderNo = jllPOInvoiceList[i].WorkOrderNo;
                    tempData.PurchaseOrderNo = jllPOInvoiceList[i].PONo;
                    tempData.AmountInHKD = (double)jllPOInvoiceList[i].TotalAmount;
                    tempData.IsReceived = jllPOInvoiceList[i].IsReceived;
                    tempData.ReceivedDate = jllPOInvoiceList[i].ReceivedDate;
                    tempData.ReceivedRef = jllPOInvoiceList[i].ReceivedRef;

                    data.Add(tempData);

                    count++;
                }
            }

            return data;
        }

        public List<QData> GetAllQuotations(List<Order> orderList)
        {
            int count = 0;
            var data = new List<QData>();

            if (orderList.Count > 0)
            {
                for (int i = 0; i < orderList.Count; i++)
                {
                    QData tempData = new QData();
                    tempData.ID = i + 1;
                    tempData.FM = orderList[i].FMCode;
                    tempData.Date = orderList[i].QuotationCreationDate.Month.ToString("00") + orderList[i].QuotationCreationDate.Day.ToString("00");
                    tempData.QuotationNo = orderList[i].QuotationNo;
                    tempData.Location = orderList[i].Location;
                    tempData.PDQuotationNo = "";
                    tempData.PDInvoiceNo = "";
                    tempData.PaymentRef = "";
                    tempData.RefNo = orderList[i].QuotationNo;
                    tempData.InvoiceNo = "";
                    tempData.InvoiceAmountInHKD = 0;
                    tempData.PaymentDate = null;
                    if (orderList[i].HasInvoice == true)
                    {
                        Invoice jllPOInv = jllPOInvoceService.GetJLLPOInvoice_ByQuotationNo(orderList[i].QuotationNo);
                        tempData.InvoiceNo = orderList[i].InvoiceNo;
                        tempData.InvoiceAmountInHKD = (double)jllPOInv.TotalAmount;
                    }

                    tempData.WorkOrderNo = orderList[i].WorkOrderNo;
                    
                    // Retrieve information from quotation No
                    Quotation quote = quoService.GetQuotationOrderbySeqNo_ByQuoteNo(orderList[i].QuotationNo);

                    if (quote != null)
                    {
                        tempData.QuotationAmountInHKD = (double)quote.TotalAmount;
                        tempData.CompletedDate = quote.WorkCompletionDate;
                        tempData.PurchaseOrderNo = quote.PONo;
                        tempData.AmountInHKD = (double)quote.TotalAmount;
                    }

                    data.Add(tempData);
                    count++;
                }
            }

            return data;
        }

        public List<WOData> GetAllWorkOrders(List<Invoice> jllOrderInvoiceList)
        {
            int count = 0;
            var data = new List<WOData>();

            if (jllOrderInvoiceList.Count > 0)
            {
                #region Company 7 - JLL Order
                for (int i=0; i<jllOrderInvoiceList.Count; i++)
                {
                    WOData tempData = new WOData();
                    tempData.ID = i + 1;
                    tempData.ServiceOrderNo = jllOrderInvoiceList[i].ServiceOrderNo;
                    tempData.Location = jllOrderInvoiceList[i].Location;
                    tempData.WorkOrderNo = jllOrderInvoiceList[i].WorkOrderNo;
                    tempData.InvoiceNo = jllOrderInvoiceList[i].InvoiceNo;
                    tempData.QuotationNo = jllOrderInvoiceList[i].QuotationNo;
                    tempData.TotalAmountInHKD = (double)jllOrderInvoiceList[i].TotalAmount;
                    //tempData.Material = "";
                    //temp

                    // Retrieve Sub-Contractor Assignment
                    tempData.SubContractor = "";
                    ContractorAssignment subContAssign = contAssignService.GetSubContAssignment_ByQuotationNo(jllOrderInvoiceList[i].QuotationNo);

                    if(subContAssign != null)
                        tempData.SubContractor = subContAssign.ContractorName;

                    data.Add(tempData);
                    count++;
                }
                #endregion
            }

            return data;
        }
    }
}